﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tema
{
    class CarFactory
    {
        private CarCoupeFactory _coupeFactory = null;
        private CarMiniFactory _miniFactory = null;
        private CarSedanFactory _sedanFactory = null;
        
        private CarPool _coupePool = null;
        private CarPool _miniPool = null;
        private CarPool _sedanPool = null;
        private int _poolSize = 1;

        public CarFactory(int poolSize)
        {
            _poolSize = poolSize;

            if (_coupePool == null)
                _coupePool = new CarPool();
            if (_coupeFactory == null)
                _coupeFactory = new CarCoupeFactory();
            

            if (_miniPool == null)
                _miniPool = new CarPool();
            if (_miniFactory == null)
                _miniFactory = new CarMiniFactory();
                        
            if (_sedanPool == null)
                _sedanPool = new CarPool();
            if (_sedanFactory == null)
                _sedanFactory = new CarSedanFactory();
        }

        public AbstractCar getCarFromPool(int type)
        {
            switch (type)
            {
                case 1: // Coupe Pool
                    if (_coupePool.getCount() != 0)
                    {
                        Console.WriteLine("Coupe returned from pool.");
                        return _coupePool.getCar();
                    }
                    else
                    {
                        Console.WriteLine("New coupe instantiated.");
                        return _coupeFactory.GetCar();
                    }
                case 2: // Mini Pool
                    if (_miniPool.getCount() != 0)
                    {
                        Console.WriteLine("Mini returned from pool.");
                        return _miniPool.getCar();
                    }
                    else
                    {
                        Console.WriteLine("New mini instantiated.");
                        return _miniFactory.GetCar();
                    }
                case 3: // Sedan Pool
                    if (_sedanPool.getCount() != 0)
                    {
                        Console.WriteLine("Sedan returned from pool.");
                        return _sedanPool.getCar();
                    }
                    else
                    {
                        Console.WriteLine("New sedan instantiated.");
                        return _sedanFactory.GetCar();
                    }
                default:
                    Console.WriteLine("Invalid car type.");
                    return null;
            }
        }

        public void releaseCar(AbstractCar car)
        {
            if (car.GetType().Name.Equals("CarCoupe"))
            {
                if (_coupePool.getCount() < _poolSize)
                    _coupePool.addCar(car);
            }
            else if (car.GetType().Name.Equals("CarMini"))
            {
                if (_miniPool.getCount() < _poolSize)
                    _miniPool.addCar(car);
            }
            else if (car.GetType().Name.Equals("CarSedan"))
            {
                if (_sedanPool.getCount() < _poolSize)
                    _sedanPool.addCar(car);
            }
            else
                Console.WriteLine("Invalid type. Could not add back to pool.");
        }

        public void generateCarPools()
        {
            for (int i = 0; i < _poolSize; i++)
            {
                _coupePool.addCar(_coupeFactory.GetCar());
                _miniPool.addCar(_miniFactory.GetCar());
                _sedanPool.addCar(_sedanFactory.GetCar());
            }
        }

        public void printPoolSizes()
        {
            Console.WriteLine("Pool Sizes: Coupe - " + _coupePool.getCount() + " Mini - " + _miniPool.getCount() + " Sedan - " + _sedanPool.getCount());
        }
    }
}
